<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Contains examples of various Kohana library examples. You can access these
 * samples in your own installation of Kohana by going to ROOT_URL/examples.
 * This controller should NOT be used in production. It is for demonstration
 * purposes only!
 *
 * $Id: examples.php 4134 2009-03-28 04:37:54Z zombor $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */

class Feedback_Controller extends Web_Controller {

	// Do not allow to run in production
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'kohana/template';
	/**
	 * Displays a list of available examples
	 */
	function index()
	{
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('feedback');
		$this->selected_page = 'feedback';;
		$this->template->content->input_error = false;
		
		if(array_key_exists('sended', $_REQUEST)){
			$this->template->content->sended = true;
		}else{
			$this->template->content->sended = false;
		}
		
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = Kohana::lang('feedback.main_title');;

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
 
		if($_POST){
			
			$data['fb_title'] = trim($_POST['title']);
			$data['fb_questing'] = trim($_POST['msg']);
			$data['fb_name'] = trim($_POST['name']);
			$data['fb_email'] = trim($_POST['mail']);
			$data['fb_date'] = date('Y-m-d H:i:s'); 
			$data['fb_ip'] = $this->input->ip_address(); 
			 
			if(!(valid::email($data['fb_email']) && valid::standard_text($data['fb_questing']) && valid::standard_text($data['fb_title']) && valid::alpha($data['fb_name']) )){ 
				$this->template->content->input_error = true;
				return ;;
			}
					
			$db = new Database;

			$db->query('SET CHARACTER SET utf8');
			$db->query('SET NAMES utf8');
			$db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");

			$status = $db->insert('bz_feedback', $data);
			 
			// count how many rows were inserted
			$rows = count($status);
			
			if ($rows > 0) {	 
				cookie::set('name', $data['fb_name']);
				cookie::set('mail', $data['fb_email']);
 
                                $swift = email::connect();
                                
                                // From, subject and HTML message 
                                $from =  new Swift_Address($data['fb_email'], $data['fb_name']);
                                $subject =  '[C-NET Feedback] '.$data['fb_title'];

                                // Build recipient lists
                                $recipients = new Swift_RecipientList;

                                $recipients->addTo(MAIL_ADMIN, 'Ivan Chura');

                                $message = new Swift_Message($subject, $data['fb_questing'], "text/html");

                                // Build the HTML message

                                if ($swift->send($message, $recipients, $from)){

                                    url::redirect('/feedback/?sended');
                                    exit();

                                }else{

                                    $this->template->content->input_error = true;
                                    return ;;

                                }
                         
			}
		}
	}
 
} ?>